package com.example.li_han.dao;

import com.example.li_han.pojo.Admin;
import com.example.li_han.utils.ConnUtils;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class AdminDao {

    /**
     * 管理员登录验证
     */
    public Admin login_check(String zhangHao, String password) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Admin admin = null;

        try {
            conn = ConnUtils.getConn();
            String sql = "SELECT * FROM admin WHERE zhanghao = ? AND password = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, zhangHao);
            ps.setString(2, password);
            rs = ps.executeQuery();

            if (rs.next()) {
                admin = new Admin(
                        rs.getInt("adminId"),
                        rs.getString("zhanghao"),
                        rs.getString("password"),
                        rs.getString("avatar")
                        // 移除 checkNum
                );
            }
        } catch (Exception e) {
            throw new RuntimeException("管理员登录验证失败", e);
        } finally {
            closeResources(conn, ps, rs);
        }
        return admin;
    }

    /**
     * 删除管理员
     */
    public int deleteAdmin(int adminId) {
        Connection conn = null;
        PreparedStatement ps = null;
        int result = 0;

        try {
            conn = ConnUtils.getConn();
            String sql = "DELETE FROM admin WHERE adminId = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, adminId);
            result = ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException("删除管理员失败", e);
        } finally {
            closeResources(conn, ps, null);
        }
        return result;
    }

    /**
     * 修改管理员信息
     */
    public int updateAdmin(Admin admin) {
        Connection conn = null;
        PreparedStatement ps = null;
        int result = 0;

        try {
            conn = ConnUtils.getConn();
            String sql = "UPDATE admin SET zhanghao = ?, password = ?, avatar = ? WHERE adminId = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, admin.getZhangHao());
            ps.setString(2, admin.getPassword());
            ps.setString(3, admin.getAvatar());
            ps.setInt(4, admin.getAdminId());
            result = ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException("修改管理员失败", e);
        } finally {
            closeResources(conn, ps, null);
        }
        return result;
    }

    /**
     * 根据ID查询管理员
     */
    public Admin getAdminById(int adminId) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Admin admin = null;

        try {
            conn = ConnUtils.getConn();
            String sql = "SELECT * FROM admin WHERE adminId = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, adminId);
            rs = ps.executeQuery();

            if (rs.next()) {
                admin = new Admin(
                        rs.getInt("adminId"),
                        rs.getString("zhanghao"),
                        rs.getString("password"),
                        rs.getString("avatar")
                        // 移除 checkNum
                );
            }
        } catch (Exception e) {
            throw new RuntimeException("查询管理员失败", e);
        } finally {
            closeResources(conn, ps, rs);
        }
        return admin;
    }

    /**
     * 查询所有管理员
     */
    public List<Admin> getAllAdmins() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Admin> adminList = new ArrayList<>();

        try {
            conn = ConnUtils.getConn();
            String sql = "SELECT * FROM admin";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();

            while (rs.next()) {
                Admin admin = new Admin(
                        rs.getInt("adminId"),
                        rs.getString("zhanghao"),
                        rs.getString("password"),
                        rs.getString("avatar")
                        // 移除 checkNum
                );
                adminList.add(admin);
            }
        } catch (Exception e) {
            throw new RuntimeException("查询所有管理员失败", e);
        } finally {
            closeResources(conn, ps, rs);
        }
        return adminList;
    }

    /**
     * 根据账号查询管理员
     */
    public Admin getAdminByZhangHao(String zhangHao) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Admin admin = null;

        try {
            conn = ConnUtils.getConn();
            String sql = "SELECT * FROM admin WHERE zhanghao = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, zhangHao);
            rs = ps.executeQuery();

            if (rs.next()) {
                admin = new Admin(
                        rs.getInt("adminId"),
                        rs.getString("zhanghao"),
                        rs.getString("password"),
                        rs.getString("avatar")
                        // 移除 checkNum
                );
            }
        } catch (Exception e) {
            throw new RuntimeException("根据账号查询管理员失败", e);
        } finally {
            closeResources(conn, ps, rs);
        }
        return admin;
    }

    /**
     * 关闭数据库资源
     */
    private void closeResources(Connection conn, PreparedStatement ps, ResultSet rs) {
        try {
            if (rs != null) rs.close();
            if (ps != null) ps.close();
            if (conn != null) ConnUtils.closeConnection(conn);
        } catch (SQLException e) {
            throw new RuntimeException("关闭数据库资源失败", e);
        }
    }
}